package sdkshell.kuaiyan.com.kuaiyansdk.util;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import sdkshell.kuaiyan.com.kuaiyansdk.BuildConfig;

/**
 * Created by wm on 2017/7/18.
 */
public class PluginUtil {
    public static PackageInfo getPackageInfo(String apkPath, Context context){
        PackageManager pm = context.getPackageManager();
        PackageInfo info = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
        return info;
    }
    /**
     * 获取当前插件的主模块版本
     * @param path
     * @return
     */
    public static double getPluginVersion(String path,Context context){
        PackageInfo packageInfo  = getPackageInfo(path,context);
        if(packageInfo == null){
            return -1.0;
        }
        try{
            double version = Double.parseDouble(packageInfo.versionName);
            return  version;
        }catch (Exception e){
            e.printStackTrace();
            return -1.0;
        }
    }

    public static int getSdkVersion(String path,Context context){
        PackageInfo packageInfo = getPackageInfo(path, context);
        if(packageInfo == null){
            return -1;
        }
        return packageInfo.versionCode;
    }

    /**
     * 检测插件对应的主工程版本与当前工程版本是否一致
     * @param path
     * @param context
     * @return
     */
    public static boolean checkVersion(String path,Context context){
        int sdkVersion = getSdkVersion(path,context);
        int localSdkVersion = BuildConfig.VERSION_CODE;
        return sdkVersion == localSdkVersion;
    }
}
